/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.front_end;

import com.trolltech.qt.core.QSize;
import com.trolltech.qt.core.Qt.AlignmentFlag;
import com.trolltech.qt.core.Qt.ConnectionType;
import com.trolltech.qt.gui.QCheckBox;
import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QSizePolicy.Policy;
import com.trolltech.qt.gui.QSpacerItem;
import com.trolltech.qt.gui.QVBoxLayout;
import csm.uttils.encrypt.CesarEncrypter;
import utn.tsb.tej.comunications.Preferences;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class PreferencesWindow extends QDialog {

    QLineEdit txtNickname, txtPort, txtPass;
    QComboBox cmbAvatar;
    QCheckBox cbPass;

    public PreferencesWindow() {
        setWindowTitle("T.E.J. - Preferences");
        setFixedSize(500, 290);

        QVBoxLayout mainLayout = new QVBoxLayout(this);

        QVBoxLayout playerLayout = new QVBoxLayout();

        //Nickname
        QHBoxLayout nicknameLayout = new QHBoxLayout();
        nicknameLayout.addWidget(new QLabel("Nickname:"));
        txtNickname = new QLineEdit(Preferences.getNickname());
        nicknameLayout.addWidget(txtNickname);

        //Avatar
        QHBoxLayout avatarLayout = new QHBoxLayout();
        avatarLayout.addWidget(new QLabel("Avatar:"));
        cmbAvatar = new QComboBox();
        cmbAvatar.setMaximumWidth(100);
        cmbAvatar.setIconSize(new QSize(65, 65));
        avatarLayout.addWidget(cmbAvatar);
        avatarLayout.setAlignment(cmbAvatar, AlignmentFlag.AlignLeft);
        loadAvatars();
        cmbAvatar.setCurrentIndex(Preferences.getAvatar());
//        cmbAvatar.currentIndexChanged.connect(this, "cmbAvatar_currentIndexChanged()");

        //GroupBox-Player
        QVBoxLayout gbPlayerLayout = new QVBoxLayout();
        gbPlayerLayout.addLayout(nicknameLayout);
        gbPlayerLayout.addLayout(avatarLayout);
        gbPlayerLayout.addSpacerItem(new QSpacerItem(5, 5, Policy.Fixed, Policy.Expanding));
        QGroupBox groupPlayerData = new QGroupBox("Player's Data:");
        groupPlayerData.setLayout(gbPlayerLayout);
        groupPlayerData.setMaximumHeight(150);

        //Port
        QHBoxLayout gbGameLayout = new QHBoxLayout();
        gbGameLayout.addWidget(new QLabel("Port:"));
        txtPort = new QLineEdit((Preferences.getPort() + ""));
        txtPort.setMaxLength(5);
        txtPort.setMaximumWidth(50);
        gbGameLayout.addWidget(txtPort);
        gbGameLayout.addWidget(new QLabel("Room Password:"));
        txtPass = new QLineEdit("");
        gbGameLayout.addWidget(txtPass);
        cbPass = new QCheckBox("Usar Pass?");
        gbGameLayout.addWidget(cbPass);
        cbPass.stateChanged.connect(this, "cbPass_stateChanged()");
        cbPass.setChecked(Preferences.isPass());
        cbPass_stateChanged();


        //GroupBox - Game
        QGroupBox groupGameData = new QGroupBox("Game Data:");
        groupGameData.setLayout(gbGameLayout);
        groupGameData.setMaximumHeight(80);

        //Accept - Cancel buttons
        QPushButton btnAccept = new QPushButton("Save Changes");
        QPushButton btnCancel = new QPushButton("Cancel");
        btnAccept.clicked.connect(this, "btnAccept_clicked()");
        btnCancel.clicked.connect(this, "btnCancel_clicked()");

        QHBoxLayout buttonsLayout = new QHBoxLayout();
        buttonsLayout.addWidget(btnAccept);
        buttonsLayout.addWidget(btnCancel);

        mainLayout.addWidget(groupPlayerData);
        mainLayout.addWidget(groupGameData);
        mainLayout.addLayout(buttonsLayout);
        Preferences.loadPreferencesFromFile("file.json");

    }

    private void btnAccept_clicked() {
        if (validateData()) {
            Preferences.setNickname(txtNickname.text());
            Preferences.setPort(Integer.parseInt(txtPort.text()));
            Preferences.setRoomPassword(new CesarEncrypter(txtPass.text()).code());
            Preferences.setAvatar(cmbAvatar.currentIndex());//TODO aca, hay que poner el numero del avatar seleccionado en la interfasoooooo
            Preferences.setPass(cbPass.isChecked());
            Preferences.savePreferencesToFile("file.json");
            this.dispose();
        }
    }

    private boolean validateData() {
        if (txtNickname.text().compareToIgnoreCase("") == 0) {
            new QMessageBox(QMessageBox.Icon.NoIcon, "Wrong Data", "You must have a Nickname! Daaaa").show();
            txtNickname.setFocus();
            return false;
        }
        if (txtPort.text().compareToIgnoreCase("") == 0) {
            new QMessageBox(QMessageBox.Icon.NoIcon, "Wrong Data", "You must have a Port! Daaaaa").show();
            txtPort.setFocus();
            return false;
        }
        return true;
    }

    private void btnCancel_clicked() {
        this.dispose();
    }

    private void loadAvatars() {
        for (int i = 0; i < 6; i++) {
            QIcon qi = new QIcon(getClass().getResource("/images/avatar/" + i + ".jpg").getPath());
            cmbAvatar.addItem(qi, "", i);
        }
    }

    private void cbPass_stateChanged() {
        if (cbPass.isChecked()) {
            txtPass.setEnabled(true);
        } else {
            txtPass.setEnabled(false);
        }
    }
}
